import { selfBuiltListAdd, selfBuiltListEdit, selfBuiltListInfo } from "@/services/aigcUser";
import { editDocTag, deleteDocTag,docTagListByPage,addDocTag ,infoDocTag} from "@/services/knowledgeFile";
import {Form, Input, Modal, message, InputNumber} from "antd";
import React from "react";
import { formatMessage } from 'umi-plugin-react/locale';
class DocLibraryTagUpdate extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            visible:false,
            title:"",
            info:null
        }
    }

    componentDidMount(){
        if(this.props.onRef){
            this.props.onRef(this)
        }
    }

    doInit = (id,title) => {
        this.setState({
            visible:true,
            title
        },() => {
            if(id){
                this.getInfo(id)
            }
        })
    }

    getInfo = async id => {
        const res = await infoDocTag({id})
        this.setState({
            info:res
        })
        this.props.form.setFieldsValue({
            tagName:res.tagName,
            weight:res.weight,
        })
    }

    doCancel = () => {
        this.setState({
            visible:false,
            info:null
        },() => {
            this.props.form.resetFields()
        })
    }

    okValidate = () => {
        const that = this;
        that.props.form.validateFields((error,values) => {
            if(!error){
                that.doOk(values)
            }
        })
    }

    doOk = async values => {
        const { info } = this.state;
        let res = {}
        if(info){
            res = await editDocTag({
                ...values,
                id:info.id,
                disabled:info.disabled
            })
        }else{
            res = await addDocTag({
                ...values,
            })
        }

        if(res.code == -1){}else{
            message.info(res.message)
            this.doCancel()
            this.props.doRefresh()
        }

    }

    checkField(rule, value, callback) {
    }

    render(){
        const { visible,title,info } = this.state;
        const { form:{getFieldDecorator} } = this.props;
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 14 },
        };
        return(
            <Modal
                visible={visible}
                onCancel={this.doCancel}
                onOk={this.okValidate}
                okText={formatMessage({ id: 'knowledgeFile.addDocLibraryTag.confirm' })}
                cancelText={formatMessage({ id: 'knowledgeFile.addDocLibraryTag.cancel' })}
                title={title}
            >
                <Form {...formItemLayout}>
                    <Form.Item label={formatMessage({ id: 'knowledgeFile.addDocLibraryTag.tagName' })}>
                        {
                            getFieldDecorator("tagName",{rules:[{required:true,message:formatMessage({ id: 'knowledgeFile.addDocLibraryTag.tagNameRequired' })}]})(
                                <Input />
                            )
                        }
                    </Form.Item>
                    <Form.Item label={formatMessage({ id: 'knowledgeFile.addDocLibraryTag.weight' })}>
                        {
                            getFieldDecorator("weight",{rules:[{required:true,message:formatMessage({ id: 'knowledgeFile.addDocLibraryTag.weightRequired' })}]})(
                                <InputNumber min={0} max={99} defaultValue={3} />
                            )
                        }
                    </Form.Item>

                </Form>
            </Modal>
        )
    }
}

export default Form.create()(DocLibraryTagUpdate)